package com.push.web.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.push.common.utils.StringUtils;
import com.push.web.service.IBizLabelFirstService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.push.common.annotation.Log;
import com.push.common.core.controller.BaseController;
import com.push.common.core.domain.AjaxResult;
import com.push.common.enums.BusinessType;
import com.push.web.domain.BizSource;
import com.push.web.service.IBizSourceService;
import com.push.common.utils.poi.ExcelUtil;
import com.push.common.core.page.TableDataInfo;

/**
 * 数据源管理Controller
 *
 * @author yanxi
 * @date 2022-03-08
 */
@RestController
@RequestMapping("/information/sources")
public class BizSourceController extends BaseController
{
    @Autowired
    private IBizSourceService bizSourceService;

    @Autowired
    private IBizLabelFirstService bizLabelFirstService;

    /**
     * 查询数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('information:sources:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizSource bizSource)
    {
        startPage();
        List<BizSource> list = bizSourceService.selectBizSourceList(bizSource);
        for (int i = 0; i < list.size(); i++) {
            if (StringUtils.isNotEmpty(list.get(i).getLabelIds())){
                List<Long> lids= Arrays.stream(list.get(i).getLabelIds().split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
                list.get(i).setLabels(bizLabelFirstService.selectBizLabelFirstByIds(lids));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('information:sources:export')")
    @Log(title = "数据源管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizSource bizSource)
    {
        List<BizSource> list = bizSourceService.selectBizSourceList(bizSource);
        ExcelUtil<BizSource> util = new ExcelUtil<BizSource>(BizSource.class);
        return util.exportExcel(list, "数据源管理数据");
    }

    /**
     * 获取数据源管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:sources:query')")
    @GetMapping(value = "/{sourceId}")
    public AjaxResult getInfo(@PathVariable("sourceId") Long sourceId)
    {
        return AjaxResult.success(bizSourceService.selectBizSourceBySourceId(sourceId));
    }

    /**
     * 新增数据源管理
     */
    @PreAuthorize("@ss.hasPermi('information:sources:add')")
    @Log(title = "数据源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizSource bizSource)
    {
        return toAjax(bizSourceService.insertBizSource(bizSource));
    }

    /**
     * 修改数据源管理
     */
    @PreAuthorize("@ss.hasPermi('information:sources:edit')")
    @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSource bizSource)
    {
        return toAjax(bizSourceService.updateBizSource(bizSource));
    }

    /**
     * 删除数据源管理
     */
    @PreAuthorize("@ss.hasPermi('information:sources:remove')")
    @Log(title = "数据源管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sourceIds}")
    public AjaxResult remove(@PathVariable Long[] sourceIds)
    {
        return toAjax(bizSourceService.deleteBizSourceBySourceIds(sourceIds));
    }
}
